$(function(){
	
	var arr_gx,arr_gy,arr_gz,arr_ax,arr_ay,arr_az;
	
	var gyro_scale = 2.0;
	var accel_scale = 20.0;
	var compass_scale = 2.0;
	var accel_calib = 10.7;
	
	arr_gx = new Array();
	arr_gy = new Array();
	arr_gz = new Array();
	arr_ax = new Array();
	arr_ay = new Array();
	arr_az = new Array();
	
	setInterval(function()
	{
		//console.log("1111111111111111111111111111111");
		$.ajax({
			type: "GET",
			url: "http://localhost:8080/at_getSensors",
			dataType: "xml",
			success: function(xml) {
				//console.log("HERE");
				if ($(xml).find("error").text()!="False")
				{
					arr_gx = new Array();
					arr_gy = new Array();
					arr_gz = new Array();
					arr_ax = new Array();
					arr_ay = new Array();
					arr_az = new Array();
					accel_x(null);
					accel_y(null);
					accel_z(null);
					gyro_x(null);
					gyro_y(null);
					gyro_z(null);
				}
				else
				{
					accel_x(parseFloat($(xml).find("ax").text()));
					accel_y(parseFloat($(xml).find("ay").text()));
					accel_z(parseFloat($(xml).find("az").text()));
					gyro_x(parseFloat($(xml).find("gx").text()));
					gyro_y(parseFloat($(xml).find("gy").text()));
					gyro_z(parseFloat($(xml).find("gz").text()));
				}
			}
		});
	},100);
	
	function accel_x(input){drawit(input,"canvas-ax",arr_ax,"#F00",accel_scale,accel_calib);}
	function accel_y(input){drawit(input,"canvas-ay",arr_ay,"#0F0",accel_scale,accel_calib);}
	function accel_z(input){drawit(input,"canvas-az",arr_az,"#00F",accel_scale,accel_calib);}
	function gyro_x(input){drawit(input,"canvas-gx",arr_gx,"#F00",gyro_scale,1);}
	function gyro_y(input){drawit(input,"canvas-gy",arr_gy,"#0F0",gyro_scale,1);}
	function gyro_z(input){drawit(input,"canvas-gz",arr_gz,"#00F",gyro_scale,1);}
	//function compass_x(input){drawit(input,"canvas-cx",arr_cx,"#F00",compass_scale);}
	//function compass_y(input){drawit(input,"canvas-cy",arr_cy,"#0F0",compass_scale);}
	//function compass_z(input){drawit(input,"canvas-cz",arr_cz,"#00F",compass_scale);}
	
	function drawit(input,surface,array,color,scale,inputCalibration)
	{
		input /= inputCalibration;
		var canvas = document.getElementById(surface);
		if (input != null)
		{
			if (array.length==50){array.shift();}
			array.push(input);
		}
		if (canvas.getContext)
		{
			var ctx = canvas.getContext('2d');
			var h = canvas.height;
			var hh = h/2;
			var w = canvas.width;
			ctx.beginPath();
			ctx.fillStyle = "#000";
			ctx.fillRect(0,0,w,h);
			
			ctx.beginPath();
			ctx.strokeStyle = "#444";
			ctx.lineWidth = 3;
			ctx.moveTo(0,hh);
			ctx.lineTo(w,hh);
			ctx.stroke();
			ctx.beginPath();
			ctx.strokeStyle = "#222";
			ctx.moveTo(0,hh/2);
			ctx.lineTo(w,hh/2);
			ctx.moveTo(0,hh*3/2);
			ctx.lineTo(w,hh*3/2);
			ctx.stroke();
			
			ctx.beginPath();
			ctx.strokeStyle = color;
			for (var i=array.length-1;i>=0;i--)
			{
				var x,y;
				x = toX(i);
				y = toY(array[i],scale);
				if (i==49){ctx.moveTo(x,y);}
				else{ctx.lineTo(x,y);}
			}
			ctx.stroke();
			
			var max,min,maxAt,minAt;
			max = array[0];
			maxAt = 0;
			min = array[0];
			minAt = 0;
			for (var i=1;i<array.length;i++)
			{
				var num = array[i];
				if (num>max)
				{
					max = num;
					maxAt = i;
				}
				else if (num<min)
				{
					min = num;
					minAt = i;
				}
			}
			
			var maxX = toX(maxAt);
			var minX = toX(minAt);
			var maxY = toY(max,scale);
			var minY = toY(min,scale);
			
			ctx.fillStyle = "yellow";
			ctx.globalAlpha = 0.8;
			
			ctx.beginPath();
			ctx.arc(maxX,maxY,5,0,2*Math.PI,false);
			ctx.fill();
			
			ctx.fillStyle = "purple";
			
			ctx.beginPath();
			ctx.arc(minX,minY,5,0,2*Math.PI,false);
			ctx.fill();
			
			ctx.globalAlpha = 1.0;
			
			ctx.textAlign = "left";
		    ctx.textBaseline = "center";
		    ctx.fillStyle = "#FFF";
			ctx.font = "20pt Calibri";
			ctx.fillText(input.toFixed(2),w/10,hh);
			
			ctx.textAlign = "center";
		    if (Math.abs(maxY-hh)>10)
		    {
		    	var text = "H"+max.toFixed(2);
		    	ctx.textBaseline = "top";
		    	ctx.font = "bold 17pt Calibri";
		    	ctx.fillStyle = "black";
		    	ctx.fillText(text,maxX,maxY-1);
		    	ctx.font = "15pt Calibri";
		    	ctx.fillStyle = "white";
		    	ctx.fillText(text,maxX,maxY-1);
		    }
			if (Math.abs(minY-hh)>10)
			{
				var text = "L"+min.toFixed(2);
				ctx.textBaseline = "bottom";
				ctx.font = "bold 17pt Calibri";
				ctx.fillStyle = "black";
				ctx.fillText(text,minX,minY+1);
				ctx.font = "15pt Calibri";
				ctx.fillStyle = "white";
				ctx.fillText(text,minX,minY+1);
			}
		}
	}
	
	function toX(input)
	{
		return (49-input)*500/49;
	}
	
	function toY(input,scale)
	{
		return max(0,min(100,50-(input*scale)));
	}
});
